--- interact_link: content/08/07/plotly8-7.ipynb kernel_name: python3 kernel_path: content/08/07 has_widgets: false title: |- Box Plots pagenum: 20 prev_page: url: /08/06/plotly8-6.html next_page: url: /08/08/plotly8-8.html suffix: .ipynb search: box id plots want plot charts groups plotlyboxplots ly python bar good graphically depicte through quartiles show outliers e points past whiskers whisker quickly compare distributions several plotlyboxplotsbasic basic plotlyboxplotscustomizing customizing comment: "***PROGRAMMATICALLY GENERATED, DO NOT EDIT. SEE ORIGINAL FILES IN /content***" ---
Box Plots

Box Plots

Box plots are good to use:

  • When graphically you want to depicte groups through their quartiles.
  • When you want to show outliers (i.e. points past the whiskers of the box and whisker plots).
  • When you want quickly compare distributions of several groups.

Basic Box Plot

import plotly.graph_objects as go
import pandas as pd
from plotly.offline import init_notebook_mode, plot
from IPython.core.display import display, HTML
init_notebook_mode(connected=True)

#Let use the cars dataset for this
mtcars = pd.read_table('https://raw.githubusercontent.com/plotly/datasets/master/mpg_2017.txt',sep='\t')


fig = go.Figure()

fig.add_trace(go.Box(y = mtcars['cty'],
                     name = 'City MPG'))

fig.add_trace(go.Box(y = mtcars['hwy'],
                     name = 'Highway MPG'))

fig.update_layout(title_text='City & Highway MPG')

plot(fig, filename = 'figure8-7-1.html')
display(HTML('figure8-7-1.html'))

Customizing Box Charts

# Additional Styling Options
fig = go.Figure()
fig.add_trace(go.Box(
    y=mtcars['cty'],
    name="All Points",
    jitter=0.3,
    pointpos=-1.8,
    boxpoints='all', # represent all points
    marker_color='rgb(7,40,89)',
    line_color='rgb(7,40,89)'
))

fig.add_trace(go.Box(
    y=mtcars['cty'],
    name="Only Whiskers",
    boxpoints=False, # no data points
    marker_color='rgb(9,56,125)',
    line_color='rgb(9,56,125)'
))

fig.add_trace(go.Box(
    y=mtcars['cty'],
    name="Suspected Outliers",
    boxpoints='suspectedoutliers', # only suspected outliers
    marker=dict(
        color='rgb(8,81,156)',
        outliercolor='rgba(219, 64, 82, 0.6)',
        line=dict(
            outliercolor='rgba(219, 64, 82, 0.6)',
            outlierwidth=2)),
    line_color='rgb(8,81,156)'
))

fig.add_trace(go.Box(
    y=mtcars['cty'],
    name="Whiskers and Outliers",
    boxpoints='outliers', # only outliers
    marker_color='rgb(107,174,214)',
    line_color='rgb(107,174,214)'
))

fig.update_layout(title_text="Box Plot Styling Outliers")

plot(fig, filename = 'figure8-7-2.html')
display(HTML('figure8-7-2.html'))